package com.fas.system.controller;

import com.fas.common.annotation.Log;
import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.model.LoginUser;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.enums.BusinessType;
import com.fas.common.utils.SecurityUtils;
import com.fas.common.utils.poi.ExcelUtil;
import com.fas.system.domain.FasContract;
import com.fas.system.service.IFasContractService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 合同管理Controller
 * 
 * @author ruoyi
 * @date 2022-04-25
 */
@Api("合同管理")
@RestController
@RequestMapping("/system/contract")
public class FasContractController extends BaseController
{
    @Autowired
    private IFasContractService fasContractService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 获取合作单位合同管理列表
     */
    @ApiOperation("获取合作单位合同管理列表")
//    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/totalList")
    public TableDataInfo totaContractlList(FasContract fasContract,Integer pageNum,Integer pageSize)    {
        System.out.println("输入的参数：" + fasContract);
        LoginUser user = SecurityUtils.getLoginUser();
        SysDept sysDept = deptService.selectDeptById(user.getDeptId());
        List<FasContract> list = new ArrayList<>();
//        DeptType :  0:总部 1:渠道商 2:维保单位
        if ("0".equals(sysDept.getDeptType())) {
            startPage();
            list = fasContractService.selectFasContractListByEngineer(fasContract);
        } else if ("2".equals(sysDept.getDeptType())) {
            fasContract.setDeptId(user.getDeptId());
            startPage();
            list = fasContractService.selectFasContractList(fasContract);
        }
        return getDataTable(list);
    }

    @ApiOperation("获取维保单位合同列表")
    @GetMapping("/maintainContractlist")
    public TableDataInfo maintainContractlist(@RequestParam(value = "condition",required = false)String condition)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        FasContract query = new FasContract();
        query.setCondition(condition);
        query.setDeptId(user.getDeptId());
        startPage();
        List<FasContract> list = fasContractService.selectFasContractList(query);
        return getDataTable(list);
    }

    /**
     * 导出合同管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FasContract fasContract)
    {
        List<FasContract> list = fasContractService.selectFasContractList(fasContract);
        ExcelUtil<FasContract> util = new ExcelUtil<FasContract>(FasContract.class);
        util.exportExcel(response, list, "合同管理数据");
    }

    /**
     * 获取合同管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(fasContractService.selectFasContractById(id));
    }

    /**
     * 新增合同管理
     */
//    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FasContract fasContract) {
        fasContract.setDeptId(getDeptId());
        return toAjax(fasContractService.insertFasContract(fasContract));
    }

    /**
     * 修改合同管理
     */
//    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FasContract fasContract)
    {
        return toAjax(fasContractService.updateFasContract(fasContract));
    }

    /**
     * 删除合同管理
     */
//    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(fasContractService.deleteFasContractByIds(ids));
    }

    /**
     * 添加合作单位合同-查询合作单位列表
     */
	@GetMapping("/companyList")
    public TableDataInfo companyList() {
        return getDataTable(fasContractService.companyList(getDeptId()));
    }


}
